package com.wx.baseio;

import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.URL;

/**
 * Java IO 相关
 * @description:
 * @author: wangxun
 * @createDate: 2021/12/21
 * @version: 1.0
 */
public class StreamTest {

    private static File file;

    @Before
    public void initFile(){
        URL resource = StreamTest.class.getClassLoader().getResource("22.txt");
        file = new File(resource.getFile());
    }


    @Test
    public void test01() throws IOException {

        InputStream is = new FileInputStream(file);

        BufferedInputStream bis = new BufferedInputStream(is);
        int num = 0;
        byte[] bytes = new byte[1024];
        // BufferedInputStream 会使用批量读取的本地方法 去加载数据
        while ((num = bis.read(bytes))!= -1){
            System.out.println(new String(bytes,0,num));
        }
    }

    @Test
    public void test02() throws IOException {

        InputStream is = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int num = 0;
        byte[] bytes = new byte[1024];
        // ByteArrayInputStream 会将字节数组当成源
        while ((num = is.read(bytes))!= -1){
            bos.write(bytes,0,num);
        }

        System.out.println(bos);
        // 清空了 可以重新使用了
        bos.reset();

        System.out.println("《-----------------------》");

        System.out.println(bos);
    }
}
